package Alibaba;

/**
 * @author zhangmin
 * @create 2022-02-02 16:42
 *
 * 557. 反转字符串中的单词 III
 * 给定一个字符串 s ，你需要反转字符串中每个单词的字符顺序，同时仍保留空格和单词的初始顺序。
 */
public class reverseWords557 {

    String reversrWord(String word){
        int left=0,right=word.length()-1;
        char[] chars = word.toCharArray();
        while (left<right){
            char temp=chars[left];
            chars[left]=chars[right];
            chars[right]=temp;
            left++;right--;
        }
        String res="";
        for (char c:chars) {
            res+=c;
        }
        return res;
    }
    public String reverseWords(String s) {
        String[] strings = s.split(" ");
        StringBuffer sb=new StringBuffer();
        for (String ss:strings) {
            String word = reversrWord(ss);
            sb.append(word).append(' ');
        }
        return sb.toString().substring(0,s.length());
    }
}
